package com.baidu.health.err;

import com.baidu.health.pojo.ResultResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@ControllerAdvice
public class GlobalExceptionHandler {
    //空指针异常
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public ResultResponse nullPointerExceptionHandler(NullPointerException ex) {
        ex.printStackTrace();
        return ResultResponse.FAILED(-1,"NullPointerException捕获了");
    }

    //类型转换异常
    @ExceptionHandler(ClassCastException.class)
    @ResponseBody
    public ResultResponse classCastExceptionHandler(ClassCastException ex) {
        ex.printStackTrace();
        return ResultResponse.FAILED(-1,"ClassCastException捕获了");
    }

    //IO异常
    @ExceptionHandler(IOException.class)
    @ResponseBody
    public ResultResponse iOExceptionHandler(IOException ex) {
        ex.printStackTrace();
        return ResultResponse.FAILED(-1,"IOException捕获了");
    }
}
